import { HOST, PORT } from "./const";
import { createStorage } from "./createStorage";
import { getDeviceUuid } from "./getDeviceUuid";
export function httpPost(path: string, body: object, isThrow: boolean = true) {
    const host = HOST;
    const port = PORT;
    let url: string = path;
    const storage = createStorage();
    const token = storage.get('token');
    if (token) {
        body['token'] = token;
    }
    body['uuid'] = getDeviceUuid()
    if (!url.startsWith(`http`)) {
        url = `http://${host}:${port}/autojs/core/autox/${path}`
    }
    const res = http.post(url, body, {
        timeout: 5 * 1000,
        contentType: 'application/json',
    }) as AutoJs.Response;
    if (!res) throw new Error(`请求失败`)
    if (res.statusCode !== 200) {
        throw new Error(`请求失败`);
    }
    if (res.headers["Content-Type"].includes('text/html')) {
        throw new Error(`请求失败ContentType类型有误`);
    }
    const resData: any = res.body.json();
    const { success, message, data } = resData;
    if (success) {
        return data;
    } else {
        if (isThrow) {
            throw new Error(message)
        } else {
            return data;
        }
    }
}
